﻿using System;
abstract class Graphics
{
    protected double area;
    //所有产品类的公共业务方法
    public double MethodSame()
    {
        //公共方法的实现
        return area;
    }

    //声明抽象业务方法
    public abstract void MethodDiff();
}

class Rectangle : Graphics
{

    //实现业务方法
    public override void MethodDiff()
    {
        Random rand = new Random();
        int width = rand.Next(20);
        int length = rand.Next(20);
        area = width * length;
       
    }
}

class Triangle : Graphics
{

    //实现业务方法
    public override void MethodDiff()
    {
        Random rand = new Random();
        double len1 = rand.NextDouble() * 7;
        double len2 = rand.NextDouble() * 7;
        double len3 = rand.NextDouble() * 7;
        if ((len1 < len2 + len3) && (len2 < len1 + len3) && (len3 < len1 + len2))
            {
                double p = (len1 + len2 + len3) / 2;
                area = Math.Sqrt(p * (p - len1) * (p - len2) * (p - len3));
            }
            else area = 0;
    }
}

class Factory
{
    //静态工厂方法
    public static Graphics GetGraphic(string arg)
    {
        Graphics product = null;
        if (arg.Equals("Triangle"))
        {
            product = new Triangle();
        }
        else if (arg.Equals("Rectangle"))
        {
            product = new Rectangle();
        }
        return product;
    }
}

class Program
{
    static void Main(string[] args)
    {
        double sum = 0;
        for(int i = 0; i < 10; i++)
        {
            Graphics product = null;
            Random random = new Random();
            int j = random.Next(100);
            if(i%2==0)
            {
                product = Factory.GetGraphic("Rectangle");
            }
            else
            {
                product = Factory.GetGraphic("Triangle");
            }
            product.MethodDiff();
            double tmp_area = product.MethodSame();
            sum += tmp_area;
            if(tmp_area == 0)
            {
                Console.WriteLine("Graphic " + i + " is illegal" );
            }
            else
            Console.WriteLine("area " + i + ":" + tmp_area);
        }
        Console.WriteLine("Total area is {0}", sum);
    }
}
